package cn.hsw.platform.core.util;

import cn.hsw.platform.core.base.BaseConstant;
import cn.hsw.platform.core.dto.ResultDto;
import cn.hsw.platform.core.exception.ApiException;
import cn.hutool.core.exceptions.ValidateException;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 验证工具
 * @author 李子木
 */
public class Validator extends cn.hutool.core.lang.Validator {

    public static final Pattern USER_NAME = Pattern.compile(BaseConstant.USER_NAME_PATTERN);
    public static final Pattern PASSWORD = Pattern.compile(BaseConstant.PASSWORD_PATTERN);

    public Validator(){ }

    public static boolean isUserName(CharSequence value) {
        return isMatchRegex(USER_NAME, value);
    }
    public static boolean password(CharSequence value) {
        return isMatchRegex(PASSWORD, value);
    }

    public static void validatePassword(CharSequence value,String errorMsg) {
        if(!password(value)){
            throw new ValidateException(errorMsg);
        }
    }

    public static boolean contains(String[] strings,String str){
        return Arrays.asList(strings).contains(str);
    }

    public static void validateContains(String[] strings,String str, String errorMsg) throws ValidateException {
        if(!Arrays.asList(strings).contains(str)){
            throw new ValidateException(errorMsg);
        }
    }

    public static <T> T validateApiSuccess(ResultDto<T> resultDto) {
        if(resultDto.isSuccess()){
            return resultDto.getData();
        }
        throw new ApiException(resultDto.getMsg());
    }

    public static <T> void validateContains(List<T> list, T t, String errorMsg) throws ValidateException {
        if(list.contains(t)){
            throw new ValidateException(errorMsg);
        }
    }
}
